const base = require("../base/controllerBase");
const moment = require("moment");
const _ = require("lodash");

class PowerController extends base {
    async copyTime() {
        // const {ctx, service} = this;
        // try {
        //
        //     const result = await ctx.app.dbs.find('PowerMeter', {});
        //     _.map(result, async item => {
        //         const {time,_time} = item;
        //         const res = await ctx.app.dbs.updateOne('PowerMeter',{time}, {timeStr: _time});
        //         console.log('dddd', time, res);
        //     });
        //
        //     ctx.status = 200;
        // } catch (e) {
        //     console.error(e);
        //     console.log(ctx.request.body);
        //     const _message = e && e.message ? e.message : this.ERROR_MSG;
        //     ctx.body = {status: 'error', message: _message, result: e};
        //     ctx.status = 200;
        // }
    }

    /**
     * 删除重复数据
     * @returns {Promise<void>}
     */
    async removeDu() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const group = {
                $group: {
                    _id: "$_time",
                    uniqueIds: {$addToSet: "$_id"},
                    count: {$sum: 1}
                }
            };
            const match = {
                $match: {
                    count: {$gt: 1}
                }
            };
            const query = [group, match];
            const result = await ctx.app.dbs.aggregate('PowerMeter', query);
            _.map(result, async (doc) => {
                doc.uniqueIds.shift();
                const res = await this.app.model['PowerMeter'].deleteOne({_id: {$in: doc.uniqueIds}});
                console.log('res', doc.uniqueIds, res);
            });

            ctx.body = result;
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async findMany() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            //
            // const model = {
            //     payload: {type: 'object'},
            //     topic: {type: 'string'}
            // };
            // ctx.validate(model, body);
            const result = await service.powerService.findMany(body);
            ctx.body = result;
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async findDayPower() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            //
            const model = {
                day: {type: 'string'}
            };
            ctx.validate(model, body);
            console.log('find power day', body);
            const result = await service.powerService.findDayPower(body);
            //  console.log('find power result',result);
            ctx.body = result;
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async exportExcel() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            //
            // const model = {
            //     day: {type: 'string'}
            // };
            // ctx.validate(model, body);
            const result = await service.powerService.exportExcel(body);
            ctx.body = result;
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async bindingDevice() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            console.log('bind device', body);
            const model = {
                tenantCode: {type: 'string'},
                deviceIds: {type: 'array'},
                treeId: {type: 'string'}
            };
            ctx.validate(model, body);
            const {deviceIds, treeId, tenantCode} = body;
            _.map(deviceIds, async (deviceId) => {
                const res = await ctx.app.dbs.updateOne("IotDevice", {deviceId, tenantCode}, {powerTreeId: treeId});
            });
            ctx.body = {status: 'ok'};
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async findBindingDeviceMany() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            //
            const model = {
                powerTreeId: {type: 'string'},
                tenantCode: {type: 'string'},
                day: {type: 'string'}
            };
            ctx.validate(model, body);
            console.log('find power day', body);

            const result = await service.powerService.findBindingDeviceMany(body);
            console.log('find power result', result);
            ctx.body = result;

            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }
}

module.exports = PowerController;
